from typing import Optional
from .base_dao import BaseDAO
from models.user import User


class UserDAO(BaseDAO[User]):
    """用户表数据访问对象"""

    def __init__(self):
        """
        初始化UserDAO
        """
        super().__init__(User)

    def _get_table_name(self) -> str:
        """获取表名"""
        return "users"

    def _get_primary_key(self) -> str:
        """获取主键字段名"""
        return "id"

    def get_by_username(self, username: str) -> Optional[User]:
        """
        根据用户名获取用户
        :param username: 用户名
        :return: 用户实体，如果不存在返回None
        """
        sql = "SELECT * FROM users WHERE username = ?"
        params = (username,)
        results = self._execute_query(sql, params)
        return results[0] if results else None



    def get_by_id_card(self, id_card: str) -> Optional[User]:
        """
        根据身份证号获取用户
        :param id_card: 身份证号
        :return: 用户实体，如果不存在返回None
        """
        sql = "SELECT * FROM users WHERE id_card = ?"
        params = (id_card,)
        results = self._execute_query(sql, params)
        return results[0] if results else None